﻿public abstract class BaseCodeWriter : ICodeWriter
{
    protected abstract string TOKEN_INDENT { get; }
    protected abstract string TOKEN_COMMENT { get; }
    protected abstract string TOKEN_BEGIN_COMMENT_MUT { get; }
    protected abstract string TOKEN_END_COMMENT_MUT { get; }
    private bool _isNewLine = true;
    public int Indent { get; set; }
    private readonly Action<string> _writer;
    public BaseCodeWriter(Action<string> writer)
    {
        _writer = writer;
    }
    public void Code(string text)
    {
        doIndent();
        _writer(text);
    }

    public void Comment(string text, bool mutLine = false)
    {
        doIndent();
        if (mutLine)
            _writer($"{TOKEN_BEGIN_COMMENT_MUT} {text} {TOKEN_END_COMMENT_MUT}");
        else
        {
            _writer($"{TOKEN_COMMENT} {text}\n");
            _isNewLine = true;
        }
    }

    public void CodeLine(string line)
    {
        doIndent();
        _writer(line + "\n");
        _isNewLine = true;
    }

    public void Line()
    {
        _writer("\n");
        _isNewLine = true;
    }

    void doIndent()
    {
        if (_isNewLine)
        {
            _isNewLine = false;
            for (var i = 0; i < Indent; i++)
                _writer(TOKEN_INDENT);
        }
    }

    public virtual void PushColor(ConsoleColor green) { }

    public virtual void PopColor() { }
}

